document.getElementById('registerForm').addEventListener('submit', function(e) {
    e.preventDefault();
    
    // 获取表单值
    const email = document.getElementById('email').value.trim();
    const password = document.getElementById('password').value;
    const confirmPassword = document.getElementById('confirmPassword').value;
    const name = document.getElementById('name').value;
    // 清除之前的错误信息
    document.getElementById('emailError').textContent = '';
    document.getElementById('passwordError').textContent = '';
    document.getElementById('confirmError').textContent = '';
    document.getElementById('nameError').textContent = '';
    // 验证邮箱
    if (!email) {
        document.getElementById('emailError').textContent = '请输入邮箱地址';
        return;
    }
    // 验证昵称
    if (!name) {
        document.getElementById('nameError').textContent = '请输入邮箱地址';
        return;
    }
    
    if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email)) {
        document.getElementById('emailError').textContent = '请输入有效的邮箱地址';
        return;
    }
    
    // 验证密码
    if (!password) {
        document.getElementById('passwordError').textContent = '请输入密码';
        return;
    }
    
    if (password.length < 8) {
        document.getElementById('passwordError').textContent = '密码长度至少8位';
        return;
    }
    
    // 验证确认密码
    if (password !== confirmPassword) {
        document.getElementById('confirmError').textContent = '两次输入的密码不一致';
        return;
    }
    
    alert('注册成功！');
    // 示例：存储到localStorage
    const userData = {
        email: email,
        name: name,
        password: password, // 实际项目中密码应该加密存储
        createdAt: new Date().toISOString()
    };
    
    // 获取现有用户数据或初始化空数组
    const users = JSON.parse(localStorage.getItem('users')) || [];
    users.push(userData);
    localStorage.setItem('users', JSON.stringify(users));
    
    // 重置表单
    this.reset();
    
    // 注册成功后跳转
    window.location.href = 'index.html';
});

// 实时验证密码强度
document.getElementById('password').addEventListener('input', function() {
    const password = this.value;
    const errorElement = document.getElementById('passwordError');
    
    if (password.length > 0 && password.length < 8) {
        errorElement.textContent = '密码强度：弱';
        errorElement.style.color = '#e74c3c';
    } else if (password.length >= 8) {
        errorElement.textContent = '密码强度：强';
        errorElement.style.color = '#2ecc71';
    } else {
        errorElement.textContent = '';
    }
});